package com.usooft.metaTalent.core.platform.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.platform.entity.Rotation;
import com.usooft.metaTalent.core.platform.mapper.RotationMapper;
import com.usooft.metaTalent.core.platform.query.RotationQuery;
import com.usooft.metaTalent.core.platform.service.RotationService;
import com.usooft.metaTalent.core.platform.wrapper.RotationWrapper;
import java.lang.Integer;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseRotationService implements RotationService {
    @Autowired
    private RotationMapper rotationMapper;

    @Override
    public Rotation get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<Rotation> find(@NonNull Long id) {
        return Optional.ofNullable(rotationMapper.get(id));
    }

    @Override
    public List<RotationWrapper> list(RotationQuery query) {
        return rotationMapper.list(query);
    }

    @Override
    public Page<RotationWrapper> page(RotationQuery query) {
        int rows = rotationMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : rotationMapper.list(query));
    }

    protected Optional<Rotation> findOne(RotationQuery.Filter filter, RotationQuery.Sorts sorts) {
        List<RotationWrapper> wrappers = list(RotationQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<Rotation> findOne(RotationQuery.Filter filter) {
        return findOne(filter, RotationQuery.Sorts.builder().build());
    }

    @Override
    public int count(RotationQuery.Filter filter) {
        return rotationMapper.count(filter);
    }

    protected Rotation internalAdd(String name, String pictureUrl, String jumpUrl, Integer sort,
            Rotation.JumpType jumpType) {
        Rotation rotation = new Rotation();
        rotation.setName(name);
        rotation.setPictureUrl(pictureUrl);
        rotation.setJumpUrl(jumpUrl);
        rotation.setSort(sort);
        rotation.setJumpType(jumpType);

        rotation.setCreatedDate(LocalDateTime.now());
        rotation.setVersion(0);
        rotation.setState(false);
        rotation.setIsDelete(false);
        rotationMapper.add(rotation);
        return rotation;
    }

    protected void internalUpdate(@NonNull Rotation rotation) {
        rotation.setLastModifiedDate(LocalDateTime.now());

        int rows = rotationMapper.update(rotation);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能轮播图 (" + rotation.getId() + ") 的版本 (" + rotation.getVersion() + ") 已不是最新");
        }
        rotation.setVersion(rotation.getVersion() + 1);
    }
}
